package com.smartcruise.main.contactsFragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.smartcruise.R;
import com.smartcruise.base.BaseFragment;
import com.smartcruise.data.Contacts;
import com.smartcruise.util.StringUtils;
import com.smartcruise.util.customView.ClearableEditTextWithIcon;
import com.smartcruise.util.sortLetter.SortModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by FengChaoQun
 * on 2018/3/6 0006
 * 搜索联系人
 */

public class SearchContactsFragment extends BaseFragment implements SearchContactsContract.View {

    @BindView(R.id.searchInput)
    ClearableEditTextWithIcon searchInput;
    @BindView(R.id.taskSearchLay)
    LinearLayout taskSearchLay;
    @BindView(R.id.search)
    TextView search;
    @BindView(R.id.listView)
    ListView listView;
    Unbinder unbinder;

    private SearchContactsContract.Presenter mPresenter;
    private List<SortModel<Contacts>> sortModels = new ArrayList<>();
    private ContactsAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getRootView(R.layout.fragment_search_contacts);
        unbinder = ButterKnife.bind(this, rootView);
        setCloseActivity();
        initView();
        return rootView;
    }

    private void initView() {
        adapter = new ContactsAdapter(getActivity(), sortModels);
        listView.setAdapter(adapter);
        mPresenter = new SearchContactPresenterImp(this);
        mPresenter.start();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.back)
    public void onBackClicked() {
        getActivity().finish();
    }

    @OnClick(R.id.search)
    public void onSearchClicked() {
        if (StringUtils.isNotEmpty(searchInput)) {
            mPresenter.search(searchInput.getText().toString());
        } else {
            showToast(getString(R.string.searchContentNotAllowEmpty));
        }
    }

    @Override
    public void showResult(List<SortModel<Contacts>> sortModels) {
        this.sortModels.clear();
        this.sortModels.addAll(sortModels);
        adapter.notifyDataSetChanged();
    }

}
